﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Gedea.Service.Services;

using System.Threading;
using System.IO.Log;
using System.IO;
using System.Text;

namespace Gedea.WinForm
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

            try
            {
                ConfigurationService.Configurate();

                if (Gedea.Service.Credentials.currentUser.UserId != 0)
                    Application.Run(new Home(true));
                else
                    Application.Run(new Gedea.WinForm.Forms.Configuration());
            }
            catch(Exception ex)
            {
                ExceptionHandling(ex);
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            ExceptionHandling(e.Exception);
        }

        private static void ExceptionHandling(Exception e)
        {
            string logDate = string.Format("{0}_{1}_{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            string fileName = string.Format("{0}_application.log", logDate);
            string message = string.Format("[{0}] Message::{1} \n StackTrace:: {2} \n\n\n\n",
                    DateTime.Now,
                    e.Message,
                    e.StackTrace);

            using (StreamWriter sr = new StreamWriter(fileName))
            {
                sr.WriteLine(message);
            }
        }
    }
}
